package com_.dxy_test_.spel_;

import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;

public class SpELTest {

    public static String test(String template, Object data){
        ExpressionParser parser = new SpelExpressionParser();
        //SPEL上下文
        Expression exp = parser.parseExpression(template, new TemplateParserContext());
        return (String) exp.getValue(data);
    }

    public static void main(String[] args) {
        String template = "{\"touser\":\"#{openid}\",\"template_id\":\"nOijOQYeQXTGUW-fSBD8TM2OzmZr8ksO46KHWLmFvwo\",\"url\":\"#{url}\",\"miniprogram\":{\"appid\":\"#{appid}\",\"pagepath\":\"#{pagePath}\"},\"data\":{\"first\":{\"value\":\"您申请办理的签证失败，护照资料退回中\",\"color\":\"#173177\"},\"keyword1\":{\"value\":\"#{arg1}签证办理\",\"color\":\"#173177\"},\"keyword2\":{\"value\":\"护照退回，派送中\",\"color\":\"#173177\"},\"keyword3\":{\"value\":\"#{arg2}\",\"color\":\"#173177\"},\"remark\":{\"value\":\"点击查看订单详情及物流进度，如有疑问可联系客服18988900373\",\"color\":\"#173177\"}}}";
        TemplateMsgBO templateMsgBO = new TemplateMsgBO()
                .setOpenid("我是openid")
                .setUrl("我是url")
                .setAppid("我是appid")
                .setPagePath("我是pagePath")
                .setArg1("我是arg1")
                .setArg2("我是arg2");
        String res = test(template, templateMsgBO);
        System.out.println(res);
    }
}
